package com.anolesoft.amljmes.fourteenDispatch.service.mapper;

import cn.hutool.core.util.StrUtil;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import com.anolesoft.amljmes.fourteenDispatch.dto.MesWpgProcessDto;
import com.anolesoft.amljmes.fourteenDispatch.controller.MesWpgProcessController;

import java.util.List;

@Mapper
public interface MesWpgProcessServiceMapping {
    // 查询列表
    @SelectProvider(type=createSQL.class, method = "queryWpgProcessSQL")
    List<MesWpgProcessDto> queryWpgProcessSQL(MesWpgProcessController.DemoVO demoVO);
    public static class createSQL {
        public static String queryWpgProcessSQL(MesWpgProcessController.DemoVO demoVO){
            return new SQL(){
                {
                    StringBuilder sql = new StringBuilder();
                    sql.append(" a.objuid as key,a.objuid, a.gongxu_name, b.name cjr, a.cjtime, c.name xgr, a.xgtime, a.factory_id, a.state,0 as pageNo,10 as pageSize  from mes_wpg_process a left join stf_employee b on a.cjr = b.objuid left join stf_employee c on a.xgr = c.objuid where a.state=0 ");
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getGongxuName())){
                        String gongxuName = demoVO.getGongxuName();
                        sql.append(" and upper(a.gongxu_name) = trim(upper(#{gongxuName, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getFactoryId())){
                        String factoryId = demoVO.getFactoryId();
                        sql.append(" and upper(a.factory_id) = upper(#{factoryId, jdbcType=VARCHAR})");
                    }
                    SELECT(sql.toString());
                }
            }.toString();
        }
    }

    // 新增
    @Insert("    insert into mes_wpg_process" +
            "      (cjr, factory_id, gongxu_name)" +
            "    values" +
            "      (#{cjr, jdbcType=VARCHAR}, #{factoryId, jdbcType=VARCHAR}, #{gongxuName, jdbcType=VARCHAR})"
    )
    int addMesWpgProcess(MesWpgProcessDto inFoDtoParam);

    // 修改
    @Update("    update mes_wpg_process" +
            "      set xgr = #{xgr, jdbcType=VARCHAR},xgtime = sysdate,gongxu_name = #{gongxuName, jdbcType=VARCHAR}" +
            "    where objuid = #{objuid, jdbcType=VARCHAR}"
    )
    int updateMesWpgProcess(MesWpgProcessDto inFoDtoParam);

    // 删除
    @Update("update mes_wpg_process set state = 1 where objuid = #{objuid, jdbcType=VARCHAR}" )
    int deleteMesWpgProcess(MesWpgProcessDto inFoDtoParam);

    //批量清除
    @Update("update mes_wpg_process set state = 1 where objuid = #{objuid, jdbcType=VARCHAR}" )
    int delete(String objuid);

    // 验证名称重复
    @Select(" select count(1) from mes_wpg_process where gongxu_name = trim(#{gongxuName, jdbcType = VARCHAR}) and factory_id = #{factoryId, jdbcType = VARCHAR} and state=0 ")
    int isMesWpgProcessName(MesWpgProcessDto inFoDtoPara);

    // 查询无需派工工序名
    @Select("select gongxu_name from mes_wpg_process where state = 0 and factory_id=#{factoryId, jdbcType=VARCHAR}")
    List<MesWpgProcessDto> gongxuNameAll(String factoryId);
}
